import { GM_setValue, GM_getValue } from "$";
import { GM_registerMenuCommand, GM_listValues, GM_deleteValue, unsafeWindow ,GM_xmlhttpRequest} from "$";
import { I_SAAS_WEB, ISystemConfig, IUserServiceForEncrypt, IAuthorizations, I_SAAS_WEB_DEBUG_INFO } from "./data-type"
import { UrlInfo } from "../../hooks/routers"
// 封装设置数据api
export const GM_setObject = function <T>(name: string, value: T) {
	GM_setValue(name, JSON.stringify(value));
}
// 封装获取数据api
export const GM_getObject = function <T>(name: string, defaultValue: T) {
	let value = GM_getValue(name, '') || ''
	try {
		return JSON.parse(value);
	} catch (e) {
		return defaultValue;
	}
};

enum I_mk_key {
	SAAS_WEB_DEBUG_INFO = "saas_web调试基础信息",
	SAAS_WEB_DEBUG_AVAILABLE = "saas_web调试开启状态",
	DEVTOOL = "开发工具",
	DEVTOOL_VISIBLE = "开发工具可见性",
	REFRESH = "清除所有浏览器缓存并刷新页面",
	BOX_TOP = "弹框距离顶部距离",
	BOX_LEFT = "弹框距离左侧距离",
	URL_INFO = "储存在浏览器内的路由跳转信息",
	SEARCH_VALUE = "路由参数",
	ERROR_COUNT = "项目报错次数",
	ACTIVE_PATHNAME = "当前激活的调试信息",
	ORIGIN_USER = "第一次登录成功储存的最原始信息",
}

interface I_keyList {
	key: string, default: any, name: I_mk_key
}
interface I_search_value {
	serverName:string,searchValue:string
}
// mk 自定义属性值获取设置函数定义
interface I_KEY_FUNCTION<T> {
	set: (value: T) => void
	get: () => T
}
// mk zi自定义属性集合定义
interface I_mkApi {
	GM_ajax?:(url:string,data:any) => any
	registerMenuCommand?: () => void
	SAAS_WEB_DEBUG_INFO?: I_KEY_FUNCTION<I_SAAS_WEB_DEBUG_INFO>
	SAAS_WEB_DEBUG_AVAILABLE?: I_KEY_FUNCTION<boolean>
	DEVTOOL_VISIBLE?: I_KEY_FUNCTION<boolean>
	REFRESH?: I_KEY_FUNCTION<boolean>
	BOX_TOP?: I_KEY_FUNCTION<number>
	BOX_LEFT?: I_KEY_FUNCTION<number>
	URL_INFO?: I_KEY_FUNCTION<UrlInfo>
	SEARCH_VALUE?:I_KEY_FUNCTION<Array<I_search_value>>
	ERROR_COUNT?:I_KEY_FUNCTION<number>
	ACTIVE_PATHNAME?:I_KEY_FUNCTION<string>
	ORIGIN_USER?:I_KEY_FUNCTION<string>
}

let mkApi: I_mkApi = {}
// mk 自定义属性列表
const keyList: I_keyList[] = [
	{ key: "SAAS_WEB_DEBUG_INFO", default: {}, name: I_mk_key.SAAS_WEB_DEBUG_INFO },
	{ key: "SAAS_WEB_DEBUG_AVAILABLE", default: false, name: I_mk_key.SAAS_WEB_DEBUG_AVAILABLE },
	{ key: "DEVTOOL_VISIBLE", default: false, name: I_mk_key.DEVTOOL_VISIBLE },
	{ key: "REFRESH", default: false, name: I_mk_key.REFRESH },
	{ key: "BOX_TOP", default: 20, name: I_mk_key.BOX_TOP },
	{ key: "BOX_LEFT", default: 50, name: I_mk_key.BOX_LEFT },
	{
		key: "URL_INFO", default: {
			path: "",
			name: "",
			fulUrl: "",
			pathName: "",
			select: "",
			port: ""
		}, name: I_mk_key.URL_INFO
	},
	{ key: "SEARCH_VALUE", default:[], name: I_mk_key.SEARCH_VALUE },
	{ key: "ERROR_COUNT", default:0, name: I_mk_key.ERROR_COUNT },
	{ key: "ACTIVE_PATHNAME", default:"", name: I_mk_key.ACTIVE_PATHNAME },
	{ key: "ORIGIN_USER", default:"", name: I_mk_key.ACTIVE_PATHNAME },
]
// 为所有自定义属性添加设置和获取数据方法
keyList.forEach((item) => {
	mkApi[item.key] = {}
	mkApi[item.key].get = function () {
		let value = GM_getValue(item.key, undefined)
		try {
			if(value == undefined) return item.default
			return JSON.parse(value);
		} catch (e) {
			return item.default;
		}
	}
	mkApi[item.key].set = function (value: any) {
		GM_setValue(item.key, JSON.stringify(value));
	}
})

mkApi.registerMenuCommand = function () {
	let dev = mkApi.DEVTOOL_VISIBLE.get()
	let tip = dev ? "仅开发模式启用此插件" : "在所有页面启用此插件"
	GM_registerMenuCommand(tip, function () {
		mkApi.DEVTOOL_VISIBLE.set(!dev)
		unsafeWindow.location.reload()
	});
	GM_registerMenuCommand("重置脚本", function () {
		const list = GM_listValues()
		if (list.length > 0) list.forEach(res => GM_deleteValue(res))
		unsafeWindow.location.reload()
	});
}

mkApi.GM_ajax = (url,data) => {
	return new Promise((resolve,reject)=>{
		GM_xmlhttpRequest({
			method: "POST",
			url,
			data:JSON.stringify(data),
			headers: {
				"Content-Type": "application/json"
			},
			onload: function(response) {
				if(response.status == 200){
					resolve(response.response)
				}else{
					reject(response)
				}
			}
		});
	})
}
export {
	mkApi,
	unsafeWindow
}